package com.gitee.freakchicken.dbapi.basic.conf;

import com.gitee.freakchicken.dbapi.basic.domain.User;
import com.gitee.freakchicken.dbapi.basic.service.C_TuHnmjxN;
import com.gitee.freakchicken.dbapi.basic.util.C_QqMwqBsa;
import com.gitee.freakchicken.dbapi.basic.util.C_3CjyYmwF;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
@Slf4j
public class C_7v4eIzSp implements HandlerInterceptor {

    @Autowired
    C_TuHnmjxN gf_BYT5mdFp;

    @Override
    public boolean preHandle(HttpServletRequest p_CoN9evcw, HttpServletResponse p_S8lwGQVk, Object p_4ZDWdDhl) throws Exception {
        log.debug(p_CoN9evcw.getServletPath());
        if (p_CoN9evcw.getMethod().equals(HttpMethod.OPTIONS)) {
            return true;
        }
        if (!(p_4ZDWdDhl instanceof HandlerMethod)) {
            return true;
        }
        PrintWriter lf_cE3QbWQn = null;
        try {
            p_S8lwGQVk.setCharacterEncoding("UTF-8");
            p_S8lwGQVk.setContentType("application/json; charset=utf-8");
            String lf_9lHwFmle = p_CoN9evcw.getHeader("Authorization");
            if (lf_9lHwFmle == null) {
                p_S8lwGQVk.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                lf_cE3QbWQn = p_S8lwGQVk.getWriter();
                lf_cE3QbWQn.append("No Token!");
                return false;
            }
            String lf_bXNNUQXt = C_QqMwqBsa.m_aBEHVjMW(lf_9lHwFmle);
            User lf_uMrWEZ8r = gf_BYT5mdFp.m_rjEo83Oq(Integer.valueOf(lf_bXNNUQXt));
            if (lf_uMrWEZ8r == null) {
                p_S8lwGQVk.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                lf_cE3QbWQn = p_S8lwGQVk.getWriter();
                lf_cE3QbWQn.append("User not exists!");
                return false;
            }
            boolean lf_1OOX7V74 = C_QqMwqBsa.m_VPeZucFy(lf_9lHwFmle, lf_uMrWEZ8r.getPassword());
            if (lf_1OOX7V74) {
                p_CoN9evcw.setAttribute("id", lf_uMrWEZ8r.getId());
                C_3CjyYmwF.m_DhbuTXdc(lf_uMrWEZ8r);
                return true;
            } else {
                p_S8lwGQVk.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                lf_cE3QbWQn = p_S8lwGQVk.getWriter();
                lf_cE3QbWQn.append("Token Invalid!");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (lf_cE3QbWQn != null) {
                lf_cE3QbWQn.close();
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest p_FKT1L5hL, HttpServletResponse p_JWKMjaVX, Object p_OyzaPv0s, ModelAndView p_g5mPgQ2F) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest p_UEGzrh0S, HttpServletResponse p_tbArJ1UT, Object p_DEHFf41V, Exception p_ScffEaEa) throws Exception {
        C_3CjyYmwF.m_FU5QOVDF();
    }
}
